﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace JohnnySins
{
    public partial class Default : System.Web.UI.Page
    {
        public string JsCodeToRunOnReady { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ConfigurationManager.AppSettings["IsDevelopmentEnvironment"] == null)
                {
                    string urlCurrent = Request.Url.ToString().ToLower();
                    if (!urlCurrent.Contains("//www."))
                    {
                        string newUrl = urlCurrent.Replace("http://", "http://www.").Replace("https://", "https://www.");
                        Response.Redirect(newUrl, false);
                    }
                }

                LoadCombo();
                if (Session["Start"] != null)
                    Response.Redirect("~/Home.aspx");
            }
        }

        private void LoadCombo()
        {
            for (int i = DateTime.Now.Year; i >= DateTime.Now.Year - 100; --i)
                ddlYearDOB.Items.Add(new ListItem(i.ToString(), i.ToString()));
            ddlYearDOB.SelectedIndex = 0;

            for (int i = 1; i <= 31; ++i)
                ddlDayDOB.Items.Add(new ListItem(i.ToString("00"), i.ToString()));
            ddlDayDOB.SelectedIndex = 0;
        }

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            Session["Start"] = null;
            DateTime dob = new DateTime(Convert.ToInt32(ddlYearDOB.SelectedValue), Convert.ToInt32(ddlMonthDOB.SelectedValue), Convert.ToInt32(ddlDayDOB.SelectedValue));
            DateTime today = DateTime.Today;
            int age = today.Year - dob.Year;
            if (dob > today.AddYears(-age)) age--;

            if (age < 18)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", string.Format("alert('{0}');", "You must be at least 18 years old!"), true);
                return;
            }

            Session["Start"] = 1;
            Response.Redirect("~/Home.aspx");
        }
    }
}